var playerData = require('../scripts/network/playerData');
var engine = require('../scripts/network/MatchvsEngine');
var ui ={
    _alert: null,
    _animSpeed: 0.3,
    jiesuanAltals: null,
    _beishu: null,
    _douzishu: null,
    _avatar: null,
    _addOrNo: null,
    _topLeft: null,
    _topRight: null,
    _topStart: null,
    _background: null,

};
//动态将精灵存储到对象ui  的 jiesuanAltals属性中
function initAltals () {
    cc.loader.loadRes('ui/jiesuanAltals/result_img', cc.SpriteAtlas, function(err, altals) {
        if(err) {
            cc.error(err);
            return;
        }
        ui.jiesuanAltals = altals.getSpriteFrames();
    })
};

function jiesuanUI (msg,mode,sex) {
    //动态加载结算界面
    cc.loader.loadRes("prefab/otherUI/jiesuan", cc.Prefab, function(err, prefab) {
        if(err) {
            cc.error(err);
            return;
        }
        ui._alert = cc.instantiate(prefab);
        cc.find('Canvas').addChild(ui._alert);
        ui._alert.position = cc.v2(0,0);
        //将结算界面上要更新 的 信息 分别绑定到我 新建的对象ui 中对应的属性
        ui._beishu = cc.find('Canvas/jiesuan/ui/beishu').getComponent(cc.Label);
        ui._douzishu = cc.find('Canvas/jiesuan/ui/douziNum').getComponent(cc.Label);
        ui._topLeft = cc.find('Canvas/jiesuan/ui/topLeft').getComponent(cc.Sprite);
        ui._topRight = cc.find('Canvas/jiesuan/ui/topRight').getComponent(cc.Sprite);
        ui._topStart = cc.find('Canvas/jiesuan/ui/topStart').getComponent(cc.Sprite);
        ui._background = cc.find('Canvas/jiesuan/ui/background').getComponent(cc.Sprite);
        ui._addOrNo = cc.find('Canvas/jiesuan/ui/addOrNo').getComponent(cc.Label);
        ui._avatar = cc.find('Canvas/jiesuan/ui/avatar').getComponent(cc.Sprite);

        ui._beishu.string = playerData.gamePlayer.beishu;
        if(msg == '地主') {
            if(mode == 1) {
                
                var num = playerData.gamePlayer.jichushu * playerData.gamePlayer.beishu;
                ui._douzishu.string = num *2;     //地主赢了获得两倍的豆子，
                playerData.presentPlayer.douziNum += num;
                if(sex == 0) {

                }else {
                    ui._avatar.spriteFrame = ui.jiesuanAltals[15];
                }
            }else {
                ui._background.spriteFrame = ui.jiesuanAltals[14];
                ui._topLeft.spriteFrame = ui.jiesuanAltals[9];
                ui._topRight.spriteFrame = ui.jiesuanAltals[8];
                ui._topStart.spriteFrame = ui.jiesuanAltals[7];
                ui._addOrNo.string = '-';

                var num =  playerData.gamePlayer.jichushu * playerData.gamePlayer.beishu;
                ui._douzishu.string = num *2;  
                playerData.presentPlayer.douziNum -= num;
                if(sex == 0) {
                    ui._avatar.spriteFrame = ui.jiesuanAltals[18];
                }else {
                    ui._avatar.spriteFrame = ui.jiesuanAltals[16];
                }
            }
            
        }else {
            if(mode == 1) {
                var num = playerData.gamePlayer.jichushu * playerData.gamePlayer.beishu;
                ui._douzishu.string = num;
                playerData.presentPlayer.douziNum += num;
                if(sex == 0) {

                }else {
                    ui._avatar.spriteFrame = ui.jiesuanAltals[15];
                }
            }else {
                ui._background.spriteFrame = ui.jiesuanAltals[14];
                ui._topLeft.spriteFrame = ui.jiesuanAltals[9];
                ui._topRight.spriteFrame = ui.jiesuanAltals[8];
                ui._topStart.spriteFrame = ui.jiesuanAltals[7];
                ui._addOrNo.string = '-';
                var num = playerData.gamePlayer.jichushu * playerData.gamePlayer.beishu;
                ui._douzishu.string = num;
                playerData.presentPlayer.douziNum -= num;
                if(sex == 0) {
                    ui._avatar.spriteFrame = ui.jiesuanAltals[18];
                }else {
                    ui._avatar.spriteFrame = ui.jiesuanAltals[16];
                }
            }
           
        }
        cc.find('Canvas/jiesuan/ui/exit').on('click',function () { 
            dismiss();
            var result = engine.prototype.leaveRoom(playerData.presentPlayer.name);
            console.log('leaveRoom Result:' + result);
           
        },this);

        cc.find('Canvas/jiesuan/ui/newGame').on('click', function () {
            dismiss();
            cc.find('Canvas').emit('newgame');
        },this);
        
    });
    
};

var dismiss = function () {
    cc.find('Canvas/jiesuan/background').opacity = 0;
    var cbFadeOut = cc.callFunc(onFadeOutFinish, this);
    var actionFadeOut = cc.sequence(cc.spawn(cc.fadeTo(ui._animSpeed,0),cc.scaleTo(ui._animSpeed, 0.5)),cbFadeOut);
    ui._alert.runAction(actionFadeOut);
}
var onFadeOutFinish = function () {
    if(ui._alert != null) {
        ui._alert.removeFromParent();
        ui._alert = null;
    }
}
module.exports = {
        jiesuanUI: jiesuanUI,
        initAltals: initAltals,
}; 